package com.daxiong.hfs;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.daxiong.hfs.bean.MainBean;
import com.daxiong.hfs.bean.ProductBean;
import com.daxiong.hfs.constant.Constant;
import com.daxiong.hfs.util.CommonUtil;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.MapBuilder;

public class MainActivity extends Activity implements OnClickListener {
	class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {

		@Override
		protected Boolean doInBackground(final String... urls) {
			try {
				final List<ProductBean> productBeanList = //
				CommonUtil.getProductBeanList(urls[0]);

				if (CollectionUtils.isEmpty(productBeanList)) {
					return Boolean.FALSE;
				}

				mainBean.setProductBeanList(productBeanList);

				return Boolean.TRUE;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return Boolean.FALSE;
		}

		@Override
		protected void onPostExecute(final Boolean result) {
			dialog.cancel();

			if (result == false) {
				final String text = "無法獲取服務器的資料，請稍候執行";
				final Context context = getApplicationContext();

				Toast.makeText(context, text, Toast.LENGTH_LONG).show();
			}
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();

			dialog = new ProgressDialog(MainActivity.this);

			dialog.setMessage("資料下載中，請稍候");
			dialog.setTitle("連接服務器");
			dialog.show();
			dialog.setCancelable(false);
		}
	}

	private Button query;

	private ProgressDialog dialog;

	private EasyTracker easyTracker;

	private MainBean mainBean = new MainBean();

	public void onClick(final View view) {
		final String category = "ui_action";
		final String action = "button_press";
		final String label = "play_button";

		final MapBuilder mapBuilder = //
		MapBuilder.createEvent(category, action, label, null);

		easyTracker.send(mapBuilder.build());

		final Intent intent = new Intent(this, ResultActivity.class);

		final EditText chineseNameEditText = (EditText) findViewById(R.id.query_conditions_chinese_name_edit_text);
		final EditText qualifiedIdEditText = (EditText) findViewById(R.id.query_conditions_qualified_id_edit_text);
		final EditText companyEditText = (EditText) findViewById(R.id.query_conditions_company_edit_text);

		final String chineseName = chineseNameEditText.getText().toString();
		final String qualifiedId = qualifiedIdEditText.getText().toString();
		final String company = companyEditText.getText().toString();

		final ProductBean productBean = //
		new ProductBean(qualifiedId, chineseName, company);

		mainBean.setQueryConditions(productBean);

		CommonUtil.processResultList(mainBean);

		if (CollectionUtils.isEmpty(mainBean.getResultList())) {
			Toast.makeText(this, "查無資料", Toast.LENGTH_LONG).show();

			return;
		}

		CommonUtil.putSerializable(intent, mainBean, Constant.MAIN_BEAN_KEY);

		startActivity(intent);
	}

	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main_layout);

		easyTracker = EasyTracker.getInstance(MainActivity.this);

		new JSONAsyncTask().execute(Constant.URL);

		setupViews();
	}

	@Override
	public void onStart() {
		super.onStart();

		EasyTracker.getInstance(this).activityStart(this);
	}

	@Override
	public void onStop() {
		super.onStop();

		EasyTracker.getInstance(this).activityStop(this);
	}

	private void setupViews() {
		query = (Button) findViewById(R.id.button_query);

		query.setOnClickListener(this);
	}
}
